package org.sport.repository;

import org.sport.model.entityModel.GrouptableEntity;
import org.sport.model.entityModel.LinkapplyhistoryEntity;
import org.sport.model.entityModel.UsertableEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Repository
public interface LinkapplyhistoryRepository extends JpaRepository<LinkapplyhistoryEntity,Integer> {




    @Transactional
    @Query("select linapp from LinkapplyhistoryEntity linapp where linapp.usertableByApplyUserId =?1")
    public List<LinkapplyhistoryEntity> findByApplyUserId(UsertableEntity owner);



    @Transactional
    @Query("select linapp from LinkapplyhistoryEntity linapp where linapp.usertableByBeApplyUserId =?1")
    public List<LinkapplyhistoryEntity> findByBeApplyUserId(UsertableEntity BeApplyUser);


    @Transactional
    @Query("select linapp from LinkapplyhistoryEntity linapp where linapp.usertableByApplyUserId =?1 and linapp.usertableByBeApplyUserId=?2")
    public LinkapplyhistoryEntity findBy2User(UsertableEntity ApplyUser,UsertableEntity BeApplyUser);



    @Transactional
    @Query("select linapp from LinkapplyhistoryEntity linapp where linapp.usertableByApplyUserId =?1 and linapp.grouptableByBeApplyGroupId=?2")
    public LinkapplyhistoryEntity findByUserAndGroup(UsertableEntity ApplyUser,GrouptableEntity BeApplyGroup);




    @Modifying
    @Transactional
    @Query("update LinkapplyhistoryEntity linapp set " +
            "linapp.status=:qStatus" +
            " where linapp.usertableByBeApplyUserId=:qBeApplyUser and linapp.usertableByApplyUserId=:qApplyUser")
    public void updateUserStatus(@Param("qStatus") String status,
                                    @Param("qBeApplyUser")  UsertableEntity beApplyUser,
                                    @Param("qApplyUser")  UsertableEntity owner);



    @Modifying
    @Transactional
    @Query("update LinkapplyhistoryEntity linapp set " +
            "linapp.status=:qStatus" +
            " where linapp.grouptableByBeApplyGroupId=:qBeApplyGroup and linapp.usertableByApplyUserId=:qApplyUser")
    public void updateGroupStatus(@Param("qStatus") String status,
                                 @Param("qBeApplyGroup") GrouptableEntity beApplyGroup,
                                 @Param("qApplyUser")  UsertableEntity owner);



}
